﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Text;
using System.Collections;
using Qreed.Drawing.TriDemo.Properties;

namespace Qreed.Drawing.TriDemo
{
    public partial class AddTextForm : Form
    {
        public AddTextForm()
        {
            InitializeComponent();
            FillListBox();
        }

        protected override void OnLoad(EventArgs e)
        {
            LoadSettings();
            comboBoxFontFamily_SelectedIndexChanged(null, EventArgs.Empty);

            base.OnLoad(e);
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            SaveSettings();

            base.OnClosing(e);
        }

        private void SaveSettings()
        {
            if (DialogResult == DialogResult.OK)
            {
                Settings.Default.AddTextFontName = FontName;
                Settings.Default.AddTextFontStyle = FontStyle;
                Settings.Default.AddTextScale = ScaleValue;
                Settings.Default.AddTextFlatten = Flatten;
                Settings.Default.AddTextText = TextValue;
            }
        }

        private void LoadSettings()
        {
            FontName = Settings.Default.AddTextFontName;
            FontStyle = Settings.Default.AddTextFontStyle;
            ScaleValue = Settings.Default.AddTextScale;
            Flatten = Settings.Default.AddTextFlatten;
            TextValue = Settings.Default.AddTextText;
        }

        public string FontName
        {
            get { return comboBoxFontFamily.SelectedItem.ToString(); }
            set
            {
                comboBoxFontFamily.SelectedIndex = comboBoxFontFamily.Items.IndexOf(value);
            }
        }

        public string FontStyle
        {
            get { return comboBoxFontStyle.SelectedItem.ToString(); }
            set { comboBoxFontStyle.SelectedIndex = comboBoxFontStyle.Items.IndexOf(value); }
        }

        public string TextValue
        {
            get { return richTextBox.Text; }
            set { richTextBox.Text = value; }
        }

        public float ScaleValue
        {
            get
            {
                float scale = 500.0f;
                float.TryParse(textBoxScale.Text, out scale);
                return scale;
            }
            set
            {
                textBoxScale.Text = value.ToString();
            }
        }

        public float Flatten
        {
            get
            {
                float flatten = 0.25f;
                float.TryParse(textBoxFlatten.Text, out flatten);
                return flatten;
            }
            set
            {
                textBoxFlatten.Text = value.ToString();
            }
        }

        private void FillListBox()
        {
            InstalledFontCollection ifc = new InstalledFontCollection();

            foreach (FontFamily ff in ifc.Families)
            {
                comboBoxFontFamily.Items.Add(ff.Name);
            }

            string[] fontStyles = Enum.GetNames(typeof(FontStyle));

            foreach (string fontStyle in fontStyles)
                comboBoxFontStyle.Items.Add(fontStyle);

            /*IEnumerator ie;
            ie = ifc.Families.GetEnumerator();
            while (ie.MoveNext())
            {
                comboBoxFontFamily.Items.Add(ie.Current.ToString());
            }*/
        }

        private void comboBoxFontFamily_SelectedIndexChanged(object sender, EventArgs e)
        {

            richTextBox.Font = BuildFontInstance();
        }

        private Font BuildFontInstance()
        {
            try
            {
                return new Font(this.FontName, 100.0f, (FontStyle)Enum.Parse(typeof(FontStyle), this.FontStyle));
            }
            catch (Exception)
            {
                return this.Font;
            }
        }
    }
}
